/**
 * 
 */
package org.wmine.visualization.data;

import java.io.File;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.wmine.core.ProjectManager;
import org.wmine.visualization.data.FrequencyDistributionData.FreqDistDataSingleClass;

import org.xml.sax.SAXException;

/**
 * @author Rajeev Kumar Thakur
 *
 */
public class FrequencyDistributionDataProvider  extends AbstractChartDataProvider{
	
	FrequencyDistributionData frequencyDistributionData;
	private String xmlFileName;
	private Document xmlDocument;
	private ArrayList<FreqDistDataSingleClass> classCollection;
	
	public FrequencyDistributionDataProvider(String fileName) throws ParserConfigurationException, SAXException, IOException {
	 
		frequencyDistributionData = new FrequencyDistributionData();
		classCollection = new ArrayList<FreqDistDataSingleClass>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = factory.newDocumentBuilder();
		if(ProjectManager.getUniqueInstance().isProjectOpened()){
			xmlDocument = (Document) parser.parse(new StringBufferInputStream(ProjectManager.getUniqueInstance().readFile(fileName)));
			parseOutputData();
		}
	}

	/**
	 * this function parses the xml file of output data and then sets the data class with
	 * that data 
	 */
	private void parseOutputData() {
		// TODO Auto-generated method stub
		NodeList fdList= xmlDocument.getElementsByTagName("fd");
		int no = fdList.getLength();
		//Element element = (Element) tableList.item(0);
		 
		//System.out.println(no);
		// TODO Auto-generated method stub
		for (int i = 0 ; i < no; i++){
			 
			Element element = (Element) fdList.item(i);
			int noOfIntervals =Integer.parseInt(getElementFromXmlFile(element,"no_of_intervals"));
			//System.out.println(noOfIntervals);
			 frequencyDistributionData.setNoOfIntervals(noOfIntervals);
					
			frequencyDistributionData.setAttributeName(getElementFromXmlFile(element,"attribute-name"));
			//System.out.println(frequencyDist.getAttributeName());
			NodeList classList = xmlDocument.getElementsByTagName("class");
			int noOfClass = classList.getLength();
			for (int j = 0; j < noOfClass; j++) {
				Element elementA = (Element) classList.item(j);
				FreqDistDataSingleClass singleClass = frequencyDistributionData.getFreqDistDataSingleClassInstance();
				double tempLower = Double.parseDouble(getElementFromXmlFile(elementA,"lower"));  
				singleClass.setLowerBound(tempLower);
				//System.out.println("Lower"+" "+singleClass.getLowerBound());
				double  tempUpper = Double.parseDouble(getElementFromXmlFile(elementA,"upper"));  
				singleClass.setUpperBound(tempUpper);
				//System.out.println("Upper"+" "+singleClass.getUpperBound());
				double  frequency = Double.parseDouble(getElementFromXmlFile(elementA,"freq"));  
				singleClass.setFrequency(frequency);
				//System.out.println("Freq"+" "+singleClass.getFrequency());
				classCollection.add(singleClass);
			}
			frequencyDistributionData.setClassList(classCollection);
		}
	}
	
	public String getElementFromXmlFile(Element elementA, String tagName){
		NodeList temp = (NodeList) elementA.getElementsByTagName(tagName);
		Element subElement = (Element) temp.item(0);
			return subElement.getFirstChild().getNodeValue();
	}

	public @Override
	Object getChartData() {
		// TODO Auto-generated method stub
		return frequencyDistributionData;
	}
	

	/**
	 * @param args this is the test main to test this code
	 */
	public static void main(String[] args)  {
		
		AbstractChartDataProvider freq;
		
		ArrayList<FreqDistDataSingleClass> classList = new ArrayList<FreqDistDataSingleClass>();
		try {
			freq = new FrequencyDistributionDataProvider("D:\\Project_Wmine\\WMine\\Freq-Dist.xml");	
			FrequencyDistributionData frqData =(FrequencyDistributionData) freq.getChartData();
			
			classList = frqData.getClassList();
		
			System.out.println(	frqData.getAttributeName());
			System.out.println(	frqData.getNoOfIntervals());
			
			for (int i = 0; i < classList.size(); i++) {
				FreqDistDataSingleClass single = null;
				single = classList.get(i);
				System.out.println(single.getFrequency());
				System.out.println(single.getLowerBound());
				System.out.println(single.getUpperBound());
			}
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
